using System;
using System.Collections;
using System.Collections.Generic;
using PixelCrushers.DialogueSystem;
using UnityEngine;
using XFABManager;
using XFGameFramework;
using XFGameFramework.SkillSystem;

public class SkillController : Controller
{

    private PlayerController playerController => Module.LoadController<PlayerController>();
    private EnemyController enemyController => Module.LoadController<EnemyController>();


    #region 属性

    private SkillConfig SkillConfig => XFABManager.AssetBundleManager.
    LoadAsset<SkillConfig>(Module.ModuleName, "SkillConfig");

    private Dictionary<int, Type> skills = new Dictionary<int, Type>();

    #endregion

    #region 方法

    //真正的释放技能
    public void ReleaseSkill(int Id,RolesModel creator, RolesModel target)
    {
        switch(Id)
        {
            case 10000:
                BasicAttack basicAttack = new BasicAttack(creator, target, Id);
                basicAttack.ApplyAction();
                break;
            case 10001:
                翅膀扇击 翅膀扇击 = new 翅膀扇击(creator, target, Id);
                翅膀扇击.ApplyAction();
                break;
            case 10002:
                怒叨 怒叨 = new 怒叨(creator, target, Id);
                怒叨.ApplyAction();
                break;
            default:
                Debug.LogError("技能Id错误");
            break;
        }
            
    }
    //初始化技能信息


    public void PlayerSkillCDcaculate()
    {
        PlayerModel playerModel = playerController.GetPlayerModel();
        for (int i = 0; i < playerModel.skillsCD.Count; i++)
        {
            if (playerModel.skillsCD[i] > 0)
            {
                playerModel.skillsCD[i]--;
            }
        }
    }
    public void EnemySkillCDcaculate()
    {
        EnemyModel enemyModel = enemyController.GetEnemyModel();
        for (int i = 0; i < enemyModel.skillsCD.Count; i++)
        {
            if (enemyModel.skillsCD[i] > 0)
            {
                enemyModel.skillsCD[i]--;
            }
        }
    }
    public void PlayerSkillCDUP(int skillindex, int cooltime)
    {
        PlayerModel playerModel = playerController.GetPlayerModel();
        playerModel.skillsCD[skillindex] = cooltime + playerModel.skillsCD[skillindex];

    }
    public void EnemySkillCDUP(int skillindex, int cooltime)
    {
        EnemyModel enemyModel = enemyController.GetEnemyModel();
        enemyModel.skillsCD[skillindex] = cooltime + enemyModel.skillsCD[skillindex];
    }
    public void skillCDUP(RolesModel model, int skillID, int cooltime)
    {
        int skillindex = 0;
        if (model is PlayerModel)
        {
            for (int i = 0; i < playerController.GetPlayerModel().skillsID.Count; i++)
            {
                if (playerController.GetPlayerModel().skillsID[i] == skillID)
                {
                    skillindex = i;
                    break;
                }
            }
            PlayerModel playerModel = model as PlayerModel;
            playerModel.skillsCD[skillindex] = cooltime + playerModel.skillsCD[skillindex];
            Debug.Log("技能CD上升，技能ID为" + skillID + "技能索引为" + skillindex + "技能CD变为" + playerModel.skillsCD[skillindex]);
        }
        else if (model is EnemyModel)
        {
            // Debug.Log("enemy skill index" + skillID);
            // Debug.Log("enemy skill id索引版" + enemyController.GetSkillList()[0]);
            // Debug.Log("enemyController.GetEnemyModel().skillsID.Count" + enemyController.GetSkillList().Count);
            for (int i = 0; i < enemyController.GetEnemyModel().skillsID.Count; i++)
            {

                if (enemyController.GetEnemyModel().skillsID[i] == skillID)
                {
                    skillindex = i;
                    break;
                }
            }
            EnemyModel enemyModel = model as EnemyModel;
            enemyModel.skillsCD[skillindex] = cooltime + enemyModel.skillsCD[skillindex];
            LogUtil.Log("技能CD上升，技能ID为" + skillID + "技能索引为" + skillindex + "技能CD变为" + enemyModel.skillsCD[skillindex]);
        }
        else
        {
            Debug.LogError("角色类型错误");
        }
    }

    public void AddSkill(PlayerModel playerModel, int skillId)
    {
        playerController.addskill(skillId);
    }
    public void AddSkill(EnemyModel enemyModel, int skillId)
    {
        enemyModel.skillsID.Add(skillId);
    }
    public List<int> GetSkillList(PlayerModel playerModel)
    {
        return playerController.GetSkillList();
    }
    public List<int> GetSkillList(EnemyModel enemyModel)
    {
        return enemyController.GetSkillList();
    }
    public List<int> GetSkillList(RolesModel model)
    {
        if (model is PlayerModel)
        {
            return GetSkillList(model as PlayerModel);
        }
        else if (model is EnemyModel)
        {
            return GetSkillList(model as EnemyModel);
        }
        else
        {
            return null;
        }
    }

    public bool CanUseSkill(int skillId)
    {
        SkillInfo skill = SkillConfig.GetSkillInfo(skillId);

        if (skill == null)
        {
            Debug.LogError("技能Id错误");
            return false;
        }

        if ((skill.minDistance <= FightInfo.Instance.Distance) && skill.maxDistance >= FightInfo.Instance.Distance )
        {
            return false;
        }
        else return true;
    }

    #endregion
}
